#include "bglLayerStack.h"

BGLLayerStack::BGLLayerStack()
{
}

BGLLayerStack::~BGLLayerStack()
{
    for (BGLLayer* layer : m_layers)
        delete layer;
}

void BGLLayerStack::PushLayer(BGLLayer* layer)
{
    m_layers.emplace(m_layers.begin() + m_layerInsertIndex, layer);
    m_layerInsertIndex++;
    layer->OnAttach();
}

void BGLLayerStack::PushOverlay(BGLLayer* overlay)
{
    m_layers.emplace_back(overlay);
    overlay->OnAttach();
}

void BGLLayerStack::PopLayer(BGLLayer* layer)
{
    auto it = std::find(m_layers.begin(), m_layers.begin() + m_layerInsertIndex, layer);
    if (it != m_layers.begin() + m_layerInsertIndex)
    {
        layer->OnDetach();
        m_layers.erase(it);
        m_layerInsertIndex--;
    }
}

void BGLLayerStack::PopOverlay(BGLLayer* overlay)
{
    auto it = std::find(m_layers.begin() + m_layerInsertIndex, m_layers.end(), overlay);
    if (it != m_layers.end())
    {
        overlay->OnDetach();
        m_layers.erase(it);
    }
}